!    
!                Parallel Sparse BLAS  version 3.5
!      (C) Copyright 2006, 2010, 2015, 2017
!        Salvatore Filippone    Cranfield University
!        Alfredo Buttari        CNRS-IRIT, Toulouse
!   
!    Redistribution and use in source and binary forms, with or without
!    modification, are permitted provided that the following conditions
!    are met:
!      1. Redistributions of source code must retain the above copyright
!         notice, this list of conditions and the following disclaimer.
!      2. Redistributions in binary form must reproduce the above copyright
!         notice, this list of conditions, and the following disclaimer in the
!         documentation and/or other materials provided with the distribution.
!      3. The name of the PSBLAS group or the names of its contributors may
!         not be used to endorse or promote products derived from this
!         software without specific written permission.
!   
!    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
!    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
!    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
!    PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PSBLAS GROUP OR ITS CONTRIBUTORS
!    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
!    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
!    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
!    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
!    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
!    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
!    POSSIBILITY OF SUCH DAMAGE.
!   
!    
! File: psb_icdasb.f90
!
! Subroutine: psb_icdasb
!   Assemble the psblas communications descriptor: inner part.
!   The user callable routine is defined in the psb_tools_mod module.
! 
! Arguments: 
!    desc  - type(psb_desc_type).    The communication descriptor.
!    info    - integer.                return code.
!    ext_hv  - logical                 Essentially this distinguishes a call 
!                                      coming from the build of an extended
!                                      halo descriptor with respect to a normal call. 
!
subroutine psb_icdasb(desc,info,ext_hv,mold)
  use psb_base_mod, psb_protect_name => psb_icdasb
  use psi_mod
#ifdef MPI_MOD
  use mpi
#endif
  implicit none
#ifdef MPI_H
  include 'mpif.h'
#endif
  !...Parameters....
  type(psb_desc_type), intent(inout) :: desc
  integer(psb_ipk_), intent(out)               :: info
  logical, intent(in), optional      :: ext_hv
  class(psb_i_base_vect_type), optional, intent(in) :: mold

  !....Locals....
  integer(psb_ipk_) ::  int_err(5)
  integer(psb_ipk_),allocatable ::  ovrlap_index(:),halo_index(:), ext_index(:)

  integer(psb_ipk_)  ::  i, n_col, dectype, err_act, n_row
  integer(psb_mpik_) ::  np,me, icomm, ictxt
  logical             :: ext_hv_
  integer(psb_ipk_) :: debug_level, debug_unit
  character(len=20)   :: name

  info = psb_success_
  int_err(1) = 0
  name = 'psb_cdasb'

  call psb_erractionsave(err_act)
  debug_unit  = psb_get_debug_unit()
  debug_level = psb_get_debug_level()

  ictxt   = desc%get_context()
  dectype = desc%get_dectype()
  n_row   = desc%get_local_rows()
  n_col   = desc%get_local_cols()
  call psb_get_mpicomm(ictxt,icomm )

  ! check on blacs grid 
  call psb_info(ictxt, me, np)
  if (np == -1) then
    info = psb_err_context_error_
    call psb_errpush(info,name)
    goto 9999
  endif

  if (.not.desc%is_ok()) then 
    info = psb_err_invalid_cd_state_
    int_err(1) = dectype
    call psb_errpush(info,name)
    goto 9999
  endif

  info = psb_get_errstatus()
  if (info /= psb_success_) then 
    ! Something went wrong in cdins/spins
    ! signal and exit
    info = psb_err_wrong_ins_
    call psb_errpush(info,name)
    goto 9999
  end if

  if (present(ext_hv)) then 
    ext_hv_ = ext_hv
  else
    ext_hv_ = .false.
  end if
  if (debug_level >= psb_debug_ext_) &
       & write(debug_unit, *) me,' ',trim(name),': start'

  if (allocated(desc%indxmap)) then 
    if (.not.ext_hv_) then 
      call psi_bld_tmphalo(desc,info)
      if (info /= psb_success_) then
        call psb_errpush(psb_err_from_subroutine_,name,a_err='bld_tmphalo')
        goto 9999
      end if
    end if

    ! Take out the lists for ovrlap, halo and ext...
    call psb_move_alloc(desc%ovrlap_index,ovrlap_index,info)
    call psb_move_alloc(desc%halo_index,halo_index,info)
    call psb_move_alloc(desc%ext_index,ext_index,info)

    if (debug_level >= psb_debug_ext_) &
         & write(debug_unit,*) me,' ',trim(name),': Final conversion'
    ! Then convert and put them back where they belong.    
    call psi_cnv_dsc(halo_index,ovrlap_index,ext_index,desc,info,mold=mold) 

    if (info /= psb_success_) then
      call psb_errpush(psb_err_from_subroutine_,name,a_err='psi_cnv_dsc')
      goto 9999
    end if

    deallocate(ovrlap_index, halo_index, ext_index, stat=info)
    if (info /= psb_success_) then
      info =psb_err_alloc_dealloc_
      call psb_errpush(info,name)
      goto 9999
    end if

    call desc%indxmap%asb(info)
    if (info == psb_success_) then 
      if (allocated(desc%indxmap%tempvg)) &
           & deallocate(desc%indxmap%tempvg,stat=info)
    end if
    if (info /= psb_success_) then 
      write(0,*) 'Error from internal indxmap asb ',info
      info = psb_success_
    end if

  else
    info = psb_err_invalid_cd_state_
    call psb_errpush(info,name)
    goto 9999
  endif

  
  if (debug_level >= psb_debug_ext_) &
       & write(debug_unit,*) me,' ',trim(name),': Done'

  call psb_erractionrestore(err_act)
  return

9999 call psb_error_handler(ione*ictxt,err_act)

  return

end subroutine psb_icdasb
